package com.weimob.cloud.api.common;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.weimob.cloud.api.exception.WeimobApiException;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Objects;

@Getter
@Setter
public class WeimobCloudRsp<T> implements Serializable {

    @JsonProperty("code")
    private CodeDTO code;

    @JsonProperty("data")
    private T data;

    @Data
    public static class CodeDTO implements Serializable {

        @JsonProperty("errcode")
        private String errcode;

        @JsonProperty("errmsg")
        private String errmsg;
    }

    public void checkResult() {
        if (this.getCode() == null) {
            throw new WeimobApiException("调用微盟接口无返回状态");
        }
        if (!Objects.equals(this.getCode().getErrcode(), "0")) {
            throw new WeimobApiException(this.getCode().getErrcode(), this.getCode().getErrmsg());
        }
    }
}
